<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>如何在多个异步任务完成时执行某项操作</title>
  <link href="../css/semantic/semantic.min.css" rel="stylesheet">
  <link href="../css/markdown.css" rel="stylesheet">
    <link href="../css/wtxynavigation.css" rel="stylesheet">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>8.如何在多个异步任务完成时执行某项操作</div><div class='content'><h1>
<a id="如何在多个异步任务完成时执行某项操作" class="anchor" href="#%E5%A6%82%E4%BD%95%E5%9C%A8%E5%A4%9A%E4%B8%AA%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E5%AE%8C%E6%88%90%E6%97%B6%E6%89%A7%E8%A1%8C%E6%9F%90%E9%A1%B9%E6%93%8D%E4%BD%9C"></a>如何在多个异步任务完成时执行某项操作?</h1>
<h2>
<a id="描述" class="anchor" href="#%E6%8F%8F%E8%BF%B0"></a>描述</h2>
<h3>
<a id="1" class="anchor" href="#1"></a>1.</h3>
<h1>
<a id="答--只需要将我们的异步任务添加到dispatch_group任务组中即可" class="anchor" href="#%E7%AD%94--%E5%8F%AA%E9%9C%80%E8%A6%81%E5%B0%86%E6%88%91%E4%BB%AC%E7%9A%84%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%B7%BB%E5%8A%A0%E5%88%B0dispatch_group%E4%BB%BB%E5%8A%A1%E7%BB%84%E4%B8%AD%E5%8D%B3%E5%8F%AF"></a>答:  只需要将我们的异步任务添加到dispatch_group任务组中即可</h1>
<ul>
<li>
<ol>
<li>创建<code>dispatch_group_t</code>, 结构体 <code>dispatch_group_t dispatchGroup = dispatch_group_create();</code>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>进入<code>dispatch_group_enter(dispatchGroup);</code>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>任务完成离开<code>dispatch_group_leave(dispatchGroup);</code>
</li>
</ol>
</li>
<li>
<ol start="4">
<li>执行最后操作<code>dispatch_group_notify</code>
</li>
</ol>
</li>
</ul>
<p>代码实现</p>
<pre><code>
- (void)test5
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    dispatch_group_t dispatchGroup = dispatch_group_create();
        
    for (int i = 0; i &lt; 2; i++) {
                        
        dispatch_group_enter(dispatchGroup);
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmvideo/20210508/fm05.mp4"];
                
        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                    
            NSLog(@"%@, ok ok, %d", [NSThread currentThread],i);
            dispatch_group_leave(dispatchGroup);
        }];
        [sessionDownloadTask resume];
    }
    
            
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀.");
    });
}


</code></pre></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>